/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Business;

import Connections.Database;
import Entities.BoothEmployee;
import Entities.BoothInformation;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Administrator
 */
public class BoothEmployeeManage extends Database{
     Connection conn=null;
    public BoothEmployeeManage()
    {
    
    }
  public boolean AddEmployee(BoothEmployee employee)
   {
      conn=getConn();
       try {
           PreparedStatement prsm=conn.prepareStatement("Insert into BoothEmployee("
                   + "name"
                   + ",phone"
                   + ",email"
                   + ",address"
                   + ",identityCardNumber"
                   + ",boothID"
                   + ",modifiedDate"
                   + ") values(?,?,?,?,?,?,?)");
            prsm.setString(1, employee.getName());
            prsm.setInt(2, employee.getPhone());
            prsm.setString(3, employee.getEmail());
            prsm.setString(4, employee.getAddress());
            prsm.setInt(5, employee.getIdentityCardNumber());
            prsm.setInt(6, employee.getBoothID());
            prsm.setDate(7, (Date) employee.getModifiedDate());
            prsm.executeUpdate();
            closeConn();
            return true;
       } catch (Exception e) {
           //e.printStackTrace();
           closeConn();
           return false;
       }
   }
 public ArrayList<String> getBoothToArrayList(){
        conn=getConn();
        ArrayList<String> arrList = new ArrayList<String>();
        try {

            PreparedStatement prsm = conn.prepareStatement("Select * from BoothInformation");
            ResultSet rs = prsm.executeQuery();
            while (rs.next()) {
                String name = rs.getInt("id")+"-"+rs.getString("name");
                arrList.add(name);
            }
           closeConn();
        } catch (SQLException ex) {
            //Logger.getLogger(BoothManage.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(null, "Get getBoothToArrayList Error!");
            closeConn();
        }
        return arrList;
    }
 public boolean UpdateBoothEmployee(BoothEmployee objBoothEmp,int id)
    {
       conn=getConn();
        try {
            PreparedStatement prsm = conn.prepareStatement("UPDATE BoothEmployee set name=?"
                    + ",phone=?"
                    + ",email=?"
                    + ",address=?"
                    + ",identityCardNumber=?"
                    + ",boothID=?"
                    + ",modifiedDate=? where id=?");
             prsm.setString(1, objBoothEmp.getName());
             prsm.setInt(2, objBoothEmp.getPhone());
             prsm.setString(3,objBoothEmp.getEmail());
             prsm.setString(4, objBoothEmp.getAddress());
             prsm.setInt(5, objBoothEmp.getIdentityCardNumber());
             prsm.setInt(6, objBoothEmp.getBoothID());
             prsm.setDate(7, (Date) objBoothEmp.getModifiedDate());
             prsm.setInt(8, id);
             prsm.executeUpdate();
             closeConn();
            return true;
        } catch (SQLException ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null, "update error !");
            closeConn();
            return false;
        }
     }
 public String getEmployeeNameById(int id){
           conn = getConn();
       String name="";
        try {

            PreparedStatement prsm = conn.prepareStatement("SELECT name FROM BoothEmployee WHERE id = ?");
            prsm.setInt(1, id);
            ResultSet rs = prsm.executeQuery();
            while(rs.next())
            {
               name=rs.getString("name");
            }
            closeConn();
        } catch (SQLException ex) {
            // ex.printStackTrace();
            JOptionPane.showMessageDialog(null,"getBoothemployee error !");
            closeConn();
            //Logger.getLogger(ExhibitorManage.class.getName()).log(Level.SEVERE, null, ex);
        }
        return name;
    }
  public ArrayList<BoothEmployee> GetListBoothEmployee()
  {
     conn=getConn();
      ArrayList<BoothEmployee> arrList=new ArrayList<BoothEmployee>();
     try{
         PreparedStatement prsm=conn.prepareStatement("Select * from BoothEmployee");
         ResultSet rs=prsm.executeQuery();
         while(rs.next())
         {
            BoothEmployee objBooth=new BoothEmployee(rs.getInt("id"), rs.getString("name")
                 ,rs.getInt("phone") , rs.getString("email"), rs.getString("address"),
                 rs.getInt("identityCardNumber"), rs.getInt("boothID"),rs.getDate("modifiedDate"));
             arrList.add(objBooth);
         }
         closeConn();
     }
     catch(Exception ex)
     {
          JOptionPane.showMessageDialog(null, "GetList error !");
          closeConn();
     }

    return arrList;
  }
 public void DeleteBoothEmpoyee(int id)
  {
     conn=getConn();
      try {
            PreparedStatement prsm=conn.prepareStatement("Delete from"
                    + " BoothEmployee where id=?");
            prsm.setInt(1, id);
            prsm.executeUpdate();
            closeConn();
      } catch (Exception e) {
          closeConn();
          JOptionPane.showMessageDialog(null, "Delete Error !");
      }

  }
    public ArrayList<BoothEmployee> SearchBoothEmployee(String name)
    {
         conn=getConn();
         ArrayList<BoothEmployee> arrayList=new ArrayList<BoothEmployee>();//'%"+str+"%'
        try {
           PreparedStatement prsm=conn.prepareStatement("Select * from BoothEmployee where name like '%"+name+"%' order by name");
           ResultSet rs=prsm.executeQuery();
           while(rs.next())
           {
               BoothEmployee objEmployee=new BoothEmployee();
               objEmployee.setId(rs.getInt("id"));
               objEmployee.setName(rs.getString("name"));
               objEmployee.setPhone(rs.getInt("phone"));
               objEmployee.setEmail(rs.getString("email"));
               objEmployee.setAddress(rs.getString("address"));
               objEmployee.setIdentityCardNumber(rs.getInt("identityCardNumber"));
               objEmployee.setBoothID(rs.getInt("boothID"));
               objEmployee.setModifiedDate(rs.getDate("modifiedDate"));
               arrayList.add(objEmployee);
           }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"GetData Failure !");
            Logger.getLogger(BoothInformation.class.getName()).log(Level.SEVERE, null, e);
        }

       return arrayList;
    }
}
